package com.xiaobingby.easyuisys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xiaobingby.common.controller.BaseController;
import com.xiaobingby.common.entity.EUDataGridQuery;
import com.xiaobingby.common.entity.EUDataGridResult;
import com.xiaobingby.common.entity.TreeNode;
import com.xiaobingby.common.utils.TreeUtil;
import com.xiaobingby.easyuisys.service.IPermissionService;
import com.xiaobingby.easyuisys.service.IRoleService;
import com.xiaobingby.sys.entity.Permission;
import com.xiaobingby.sys.entity.Role;
import com.xiaobingby.sys.entity.ZNodes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 李磊
 * @date 2019/5/8
 */
@Controller("easyuiRoleController")
@RequestMapping("/easyui")
public class RoleController extends BaseController {
    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;
    @Autowired
    com.xiaobingby.sys.service.IPermissionService sysPermissionService;

    @GetMapping("/role/list")
    public String roleIndex(){
        return "/easyui/role/roleManager";
    }

    @PostMapping("/role/getList")
    @ResponseBody
    public EUDataGridResult listRoles(EUDataGridQuery query,String searchText){
        EntityWrapper<Role> ew = new EntityWrapper<>();
        ew.like("name",searchText);
        Page page = roleService.selectPage(query.getPage(),ew);
        return EUDataGridResult.newInstance(page);
    }

    @GetMapping("/role/permissionSet")
    public String permissionSet(long id, Model model){
        model.addAttribute("id",id);
        return "easyui/role/permissionSet";
    }

    @GetMapping("/role/permissionTree")
    @ResponseBody
    public List<TreeNode> permissonTree(long rid){
        List<ZNodes> permissionZTreeNodes = sysPermissionService.findPermissionZTreeNodes(rid);
        return TreeUtil.getTrees(permissionZTreeNodes,false,true);
    }

    @GetMapping("/role/addRole")
    public String addRole(){
        return "/easyui/role/roleAdd";
    }

    @GetMapping("/role/modify")
    public String modify(Role role,Model model){
        model.addAttribute("role",role);
        return "/easyui/role/roleModify";
    }
}
